/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.iss.examsys.view;

import com.iss.examsys.ejb.UserBean;
import com.iss.examsys.entity.User;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import javax.inject.Named;

/**
 *
 * @author User
 */
@RequestScoped
@Named
public class UserView {
    
     @EJB UserBean userBean;
    
    private Long userId;
    private User selectedUser = new User();
    private List<User> selectedUsers = new ArrayList<User>();
    
    private User newUser = new User();
    private List<User> allUser = new ArrayList<User>();

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public User getSelectedUser() {
        return selectedUser;
    }

    public void setSelectedUser(User selectedUser) {
        this.selectedUser = selectedUser;
    }

    public User getNewUser() {
        return newUser;
    }

    public void setNewUser(User newUser) {
        this.newUser = newUser;
    }

    public List<User> getSelectedUsers() {
        return selectedUsers;
    }

    public List<User> getAllUser() {
        return allUser;
    }
    
    public List<User> getAllStudents(){
        return userBean.findUsersByRoles("students");
    }
    
    public void createUser(){
        System.out.println("Create user : " +newUser.toString());
 
        userBean.create(newUser);
    }
    
    public void editUser(){
        System.out.println("Edit user : " +selectedUser.toString());

        userBean.edit(selectedUser);
    }
    
    public void deleteUser(){
        System.out.println("Delete user : " +selectedUser.toString());
              
        userBean.remove(selectedUser);
    }
    
     public void validateUserId(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        
        UIInput inputId = (UIInput)component.findComponent("id");
        
        User user;
                
        user = userBean.findByUserId((String)value);
         
        if (user!=null) {
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "User Id already in use, choose another", null));
        }
    }
     
    public void validateUserEmail(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        
        UIInput inputId = (UIInput)component.findComponent("id");
        
        User user;
                
        user = userBean.findByUserEmail((String)value);
           
        if (user!=null) {
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "User Email already in use, choose another", null));
        }
    }
}
